package com.mq.transaction;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ytj
 * @date 2024/1/23
 */
@Configuration
public class Demo12Config {

    @Bean
    public Queue demo12Queue() {
        return new Queue(Demo12Message.QUEUE_NAME,true,false,false);
    }

    @Bean
    public DirectExchange demo12DirectExchange(){
        return new DirectExchange(Demo12Message.EXCHANGE_NAME,true,false);
    }

    @Bean
    public Binding demo12Binding(){
        return BindingBuilder.bind(demo12Queue()).to(demo12DirectExchange()).with(Demo12Message.ROUTING_KEY);
    }

    @Bean
    public RabbitTransactionManager rabbitTransactionManager(ConnectionFactory connectionFactory, RabbitTemplate rabbitTemplate){
        //配置支持事务
        rabbitTemplate.setChannelTransacted(true);
        //创建对象
        return new RabbitTransactionManager(connectionFactory);
    }

}
